

import Taro from "@tarojs/taro";

export const setStorage = (key: string, value: any) => {
    return new Promise((resolve, reject) => {
        Taro.setStorage({ key, data: JSON.stringify(value), success: resolve, fail: reject });
    });
};

export const getStorage = async (key) => {
    const res = await Taro.getStorage({
        key
    }).catch(err=>{
        console.log(key,err.errMsg || "本地数据出现错误");
    });
    let result = null;
    if(res && res.data){
        try {
            result = JSON.parse(res.data);
        } catch (error) {
            console.error(`本地数据异常${error}`);
        }
    }
    return result;
};